<template>
  <div class="photo-con">
    <!-- 顶部区域 -->

    <div class="">
          <a v-for="item in categoryList" :key="item.id"
            @click="onClickGetPhotoListByCategoryId(item.id)">
            {{item.title}}
          </a> 
    </div>
    <!-- 图片列表区域 -->
    <ul class="photo-list">
      <router-link v-for="item in photoList" :key="item.id" :to="{ name: 'photo_show', params: { id: item.id }}" tag="li">
        <img v-lazy="`${APIBaseURL}${item.img}`"> <!-- 使用图片懒加载 -->
        <div class="info">
          <h1 class="info-title">{{ item.title }}</h1>
          <div class="info-body">{{ item.summary }}</div>
        </div>
      </router-link>
    </ul>
  </div>
</template>

<script>
import {HTTPGetPhotoCategory,HTTPGetPhotoListByCategoryId} from "@/api/photo.js"
export default {
  data () {
    return {
      categoryList: [],// 所有分类的列表数组
      photoList: [], // 图片列表的数组
      categoryId: 0
    }
  },
  
  created () {
    // 调用方法
    this.getAllCategory();
    this.onClickGetPhotoListByCategoryId(0)
  },
  methods: {
    // 获取图片的分类
    async getAllCategory () {
      try {
        let result = await HTTPGetPhotoCategory();
        if (result.code === 1) {
          this.categoryList = result.data;
        }else throw result.msg;
      } catch (error) {
        this.$messagebox("错误",error)
      }      
    },//==============================================
    async onClickGetPhotoListByCategoryId (categoryId) {
      // console.log(categoryId)
      this.categoryId = categoryId
      // 根据分类Id,获取图片列表
      try {
        let result = await HTTPGetPhotoListByCategoryId({category_id: categoryId});     
        // console.log(result)   
        if (result.code == 1) {
          this.photoList = result.data;
          if(this.photoList.length==0){
            this.$toast("没有记录数据")
          }
        }else throw result.msg;
      } catch (error) {
        this.$messagebox("错误",error)
      }  
    },//==============================================
  }
}
</script>

<style lang="scss">
*{
  touch-action: pan-y; /*去除警告*/
}
.photo-list {
  list-style: none;
  margin: 0;
  padding: 10px;
  padding-bottom: 0;
  li {
    background-color: #ccc;
    text-align: center;
    margin-bottom: 5px;
    box-shadow: 0 0 9px #999;
    position: relative;
    img {
      width: 100%;
      vertical-align: middle;
    }
    img[lazy="loading"] {
      width: 40px;
      height: 300px;
      margin: auto;
    }
    .info {
      color: white;
      text-align: left;
      position: absolute;
      bottom: 0;
      background-color: rgba(0, 0, 0, 0.4);
      max-height: 84px;
      .info-title {
        font-size: 14px;
      }
      .info-body {
        font-size: 13px;
      }
    }
  }
}
</style>

